package cn.levi.excel.validate;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.levi.excel.annotation.Excell;
import cn.levi.excel.module.StateBo;
import java.lang.reflect.Field;
import java.util.Map;
import org.springframework.stereotype.Component;

/**
 * 必填校验
 *
 * @author lei
 * @since 2024/9/19
 */
@Component
public class ValidateRequired implements ValidateExcelImport{


	/**
	 * 校验必填
	 *
	 * @param field         字段
	 * @param excellDataRow excel一行数据 其中key已经转还为bean属性 但是value为excel的原始数据，
	 * @return
	 */
	@Override
	public StateBo validate(Field field, Map<String, Object> excellDataRow) {
		Excell annotation = field.getAnnotation(Excell.class);
		if (annotation==null){
			return StateBo.success();
		}
		Object value = excellDataRow.get(field.getName());
		if (annotation.required() && (ObjectUtil.isNull(value) ||"".equals(value))) {
			return StateBo.fail(StrUtil.format("{}不能为空",annotation.value()));
		}
		return StateBo.success();
	}

}
